package cn.bupt.sse309.ydl.activity;

import cn.bupt.sse309.ydl.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;

public class LoginActivity extends Activity implements  OnClickListener {

  private Button registerBtn, loginBtn,backBtn;
  private TextView fogetPasswordTv,titleTv;
  private EditText usernameEt, userPasswordEt;

  private String mAccount;
  private String mPwd;
  private SharedPreferences sp;

  @Override
  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    this.setContentView(R.layout.activity_login);
    this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.base_title_with_back);
    this.initView();

  }

  public void initView() {
    this.sp = this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE);
    this.usernameEt = (EditText) this.findViewById(R.id.et_username_login);
    this.userPasswordEt = (EditText) this.findViewById(R.id.et_password_login);
    this.usernameEt.setText(this.sp.getString("USER_NAME", ""));
    this.userPasswordEt.setText(this.sp.getString("PASSWORD", ""));
    this.registerBtn = (Button) this.findViewById(R.id.btn_register);
    this.loginBtn = (Button) this.findViewById(R.id.btn_login);
    this.fogetPasswordTv= (TextView) this.findViewById(R.id.tv_forgot_password);


    this.backBtn=(Button) this.findViewById(R.id.baseBack);
    this.titleTv = (TextView) this.findViewById(R.id.baseTitleId);
    this.titleTv.setText(R.string.title_user_login);
    this.fogetPasswordTv.setClickable(true);
    this.fogetPasswordTv.setFocusable(true);
    this.fogetPasswordTv.setOnClickListener(this);
    this.registerBtn.setOnClickListener(this);
    this.loginBtn.setOnClickListener(this);
    this.backBtn.setOnClickListener(this);

  }



  /* (non-Javadoc)
   * @see android.view.View.OnClickListener#onClick(android.view.View)
   */
  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.btn_register:

      Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
      LoginActivity.this.startActivity(intent);
      break;
    case R.id.btn_login:
      System.out.println("登录");
      LoginActivity.this.mAccount = LoginActivity.this.usernameEt.getText().toString();
      LoginActivity.this.mPwd = LoginActivity.this.userPasswordEt.getText().toString();
      LoginActivity.this.login();
      break;
    case R.id.tv_forgot_password:
      Intent intent2 = new Intent(LoginActivity.this, ForgetPasswActivity.class);
      LoginActivity.this.startActivity(intent2);
      break;
      case R.id.baseBack:
   //     LoginActivity.this.finish();
        break;
    default:
      break;
    }
  }

  public void login() {

    HashMap<String, String> urlParams = new HashMap<String, String>();
    if (this.usernameEt.length() > 0 && this.userPasswordEt.length() > 0) {
      urlParams.put("phoneNumber", this.usernameEt.getText().toString());
      urlParams.put("password",  this.userPasswordEt.getText().toString());

      Editor editor = this.sp.edit();
      editor.putString("USER_NAME", this.mAccount);
      editor.putString("PASSWORD",this.mPwd);
      editor.commit();

    } else {
      Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
      return;
    }



  }
}
